package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import org.junit.jupiter.api.Test;

import com.mysql.cj.protocol.Resultset;

public class JDBCUtils {

	private static String url;
	private static String user;
	private static String password;
	private static String driver;
	
	static {
		ResourceBundle r=ResourceBundle.getBundle("tushu.properties.jdbc");
		
		url=r.getString("url");
		user=r.getString("user");
		password=r.getString("password");
		driver=r.getString("driver");
//		System.out.println(url);
//		System.out.println(user);
//		System.out.println(password);
//		System.out.println(driver);
		
		try {
			//1-加载驱动
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	//2-建立链接
	public static Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url,user,password);
	}
	
	public static void close(ResultSet rs, Statement st,Connection conn) {
		
		if (rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (st!=null) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
